<script setup>
import { ref, onMounted } from "vue";
import axios from "axios";
import { ElDialog, ElTable, ElTableColumn, ElButton } from "element-plus";
import "element-plus/dist/index.css";
import { useRoute, useRouter } from "vue-router";

const showIndicatorDialog = ref(false);
const indicatorLoading = ref(false);
const indicatorError = ref("");
const indicatorList = ref([]);
const route = useRoute();
const router = useRouter();
const planName = ref(route.query.q || '');

// 计划相关
const planList = ref([]);
const loading = ref(false);
const error = ref("");

async function fetchIndicators(planId) {
  indicatorLoading.value = true;
  indicatorError.value = "";
  indicatorList.value = [];
  try {
    const res = await axios.get("http://localhost:8080/home/searchPlan/indicators", {
      params: { planId },
    });
    if (res.data.code === 200) {
      indicatorList.value = res.data.data.indicators;
    } else {
      indicatorError.value = res.data.message || "获取指标失败";
    }
  } catch (e) {
    indicatorError.value = "获取指标失败";
  } finally {
    indicatorLoading.value = false;
  }
}

async function fetchPlans() {
  loading.value = true;
  error.value = "";
  try {
    const res = await axios.get("http://localhost:8080/home/search", {
      params: { planName: planName.value }
    });
    if (res.data.code === 200) {
      planList.value = res.data.data || [];
    } else {
      error.value = res.data.message || "获取计划失败";
    }
  } catch (e) {
    error.value = "获取计划失败";
  } finally {
    loading.value = false;
  }
}

function openIndicatorDialog(planId) {
  showIndicatorDialog.value = true;
  fetchIndicators(planId);
}

function goHome() {
  router.push("/");
}

onMounted(() => {
  fetchPlans();
});
</script>

<template>
  <div class="bank-home-v2">
    <!-- 顶部横幅 -->
    <div class="bank-header-v2">
      <img
        class="bank-logo-v2"
        src="https://img.icons8.com/ios-filled/100/2d5fa4/bank.png"
        alt="银行logo"
      />
      <span class="bank-title-v2">黄氏银行业务平台</span>
      <input class="bank-search-v2" placeholder="输入关键词搜索" disabled />
    </div>
    <div class="search-result-bg">
      <div class="home-title-bar" @click="goHome">
        <span class="home-icon">
          <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="#2d5fa4" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 11L12 4l9 7"/><path d="M9 22V12h6v10"/><path d="M21 22H3"/></svg>
        </span>
        <span class="home-title-text">网站首页</span>
      </div>
      <div v-if="loading">加载中...</div>
      <div v-else-if="error" style="color: red">{{ error }}</div>
      <div v-else>
        <template v-if="planList.length > 0">
          <div v-for="plan in planList" :key="plan.planId" style="margin-bottom: 40px; cursor:pointer;" @click="openIndicatorDialog(plan.planId)">
            <div style="font-size: 1.2rem; font-weight: bold;">
              微专业 | "{{ plan.planName }}"
            </div>
            <div style="color: #888; margin: 8px 0 0 0;">
              {{ plan.startDate }} - {{ plan.endDate }}
            </div>
            <hr style="margin-top: 20px; border: 1px dashed #bbb;" />
          </div>
        </template>
        <template v-else>
          <div class="no-data-tip">无数据</div>
        </template>
      </div>
    </div>
    <el-dialog
      v-model="showIndicatorDialog"
      title="考核指标"
      width="700px"
      :close-on-click-modal="false"
    >
      <template #default>
        <div v-if="indicatorLoading">加载中...</div>
        <div v-else-if="indicatorError" style="color: red">{{ indicatorError }}</div>
        <el-table v-else :data="indicatorList" style="width: 100%">
          <el-table-column prop="indicatorId" label="编号" width="100" />
          <el-table-column prop="indicatorName" label="名称" width="180" />
          <el-table-column prop="indicatorType" label="类型" width="120" />
          <el-table-column prop="scoringStandard" label="评分标准" />
          <el-table-column prop="weight" label="权重" width="100" />
        </el-table>
      </template>
      <template #footer>
        <el-button @click="showIndicatorDialog = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.bank-home-v2 {
  min-height: 100vh;
  background: #e3eefc;
  font-family: "Microsoft YaHei", Arial, sans-serif;
}
.bank-header-v2 {
  display: flex;
  align-items: center;
  background: #6ea8e5;
  color: #fff;
  padding: 0.5rem 2rem;
  height: 70px;
}
.bank-logo-v2 {
  width: 54px;
  height: 54px;
  margin-right: 1.2rem;
}
.bank-title-v2 {
  font-size: 2.3rem;
  font-weight: bold;
  letter-spacing: 2px;
  flex: 1;
}
.bank-search-v2 {
  border: none;
  border-radius: 0;
  padding: 0.6rem 1.2rem;
  font-size: 1.1rem;
  outline: none;
  width: 240px;
}
.search-result-bg {
  margin: 40px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 24px 0 rgba(0,0,0,0.08);
  padding: 32px 40px 24px 40px;
}
.home-title-bar {
  display: flex;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
  margin-bottom: 20px;
  cursor: pointer;
  user-select: none;
  color: #222;
  transition: color 0.2s;
}
.home-title-bar:hover {
  color: #2d5fa4;
}
.home-icon {
  font-size: 2.1rem;
  margin-right: 12px;
  display: flex;
  align-items: center;
}
.home-title-text {
  font-size: 2rem;
}
.no-data-tip {
  color: #aaa;
  text-align: center;
  font-size: 1.2rem;
  margin: 40px 0;
}
</style>
